class PhotosController < ApplicationController
  before_filter [:init_custom_includes, :init_trace_nav]
  before_filter :find_photo,
    :only => [:show, :update, :sequential_edit, :sequential_update,
		:edit, :destroy, :set_as_entry_thumbnail]
  before_filter :authenticate,
    :only => [:update, :set_as_entry_thumbnail, :sequential_edit,
    :sequential_update, :edit, :destroy]

  def show
		@custom_stylesheets << "photos.show"
    @custom_scripts << "jquery-1.4.2" << "expand"
    @trace_nav << {:path => entries_path, :label => 'Entry Index'} <<
      {:path => entry_url(@photo.entry),
        :label => "&quot;#{@photo.entry.title}&quot;"}
	  @next_photo = next_photo(@photo)
		@previous_photo = previous_photo(@photo)
  end

  def edit
	  @save_action = 'update'
  end

  def sequential_edit
    @up_one_level_path = "/entries/#{@photo.entry.id}/edit"
    @up_one_level_label = 'Back to Entry'
    @trace_nav << {:path => entries_path, :label => 'Entry Index'} <<
      {:path => entry_url(@photo.entry),
        :label => "&quot;#{@photo.entry.title}&quot;"} <<
      {:path => "#{entry_url(@photo.entry)}/edit", :label => "Edit"}
    @custom_stylesheets << "photos.sequential_edit"
	  @save_action = 'sequential_update'
  end

  def update
    if @photo.update_attributes(params[:photo])
      redirect_to entry_photo_url(@photo.entry, @photo)
    else
      render :action => "edit"
    end
  end

  def set_as_entry_thumbnail
    if @photo.set_as_entry_thumbnail
      flash[:notice] = "Entry thumbnail successfully set"
    end
    redirect_to "#{photo_url(@photo)}/sequential_edit"
  end

	def sequential_update
    if @photo.update_attributes(params[:photo])
			next_to_edit = next_photo(@photo)
			if next_to_edit == nil
			  redirect_to edit_entry_url(@photo.entry)
			else
				redirect_to sequential_edit_photo_url(next_to_edit)
		  end
	  else
		  redirect_to edit_entry_url(@photo.entry)
		end
	end
  
  def destroy
    @photo.destroy
  end

  private
    def find_photo
      if Photo.exists?(params[:id])
        @photo = Photo.find(params[:id])
      else
        flash[:notice] = "Photo not found."
				respond_to do |format|
					format.html { redirect_to entry_path(@photo.entry) }
					format.xml { head :ok }
				end
      end
    end

    def authenticate
      if current_user && @photo.owner == current_user
        return true
      else
        flash[:notice] = "You don't have permission to do that!"
				respond_to do |format|
					format.html { redirect_to entry_path(@photo.entry) }
					format.xml { head :ok }
				end
      end
    end

		def next_photo(current_photo)	
			next_photo = nil
			if false # current_photo.shot_date_time
				if !current_photo.chronological_last_in_entry?
					next_photo = current_photo.chronological_next_in_entry
				end
			else
				next_photo = current_photo.next_in_entry_by_filename
			end
			return next_photo
		end

		def previous_photo(current_photo)
			previous_photo = nil
			if current_photo.shot_date_time
				if !@photo.chronological_first_in_entry?
					previous_photo = current_photo.chronological_previous_in_entry
				end
			else
				previous_photo = current_photo.previous_in_entry
			end
			return previous_photo
		end	
end
